import {
  BadGatewayException,
  CallHandler,
  ExecutionContext,
  Injectable,
  NestInterceptor,
} from "@nestjs/common";
import { catchError, map, Observable, throwError } from "rxjs";
import { Constants } from "../../../types/types";

@Injectable()
export class MapResInterceptor implements NestInterceptor {
  intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
    return (
      next
        .handle()
        // .pipe(
        //   map((res) => {
        //       if (res) {
        //         const {
        //           data = "", code = Constants.SuccessCode, msg = "success"
        //         } = res;
        //         return {
        //           data: data,
        //           code: code,
        //           msg: msg
        //         };
        //       }
        //     return {
        //       data: "",
        //       code: Constants.SuccessCode,
        //       msg: "success"
        //     };
        //
        //     }
        //   )
        // )
        .pipe(
          catchError((err) =>
            throwError(() => new BadGatewayException(err.message)),
          ),
        )
    );
  }
}
